package com.go.where.family.component.impl;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.CustomerFeedbackComponent;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.CustomerFeedbackInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CustomerFeedbackService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.CustomerVo;
import com.google.gson.Gson;

@Component("customerFeedbackComponent")
public class CustomerFeedbackComponentImpl implements CustomerFeedbackComponent {

	private static final Logger log = Logger
			.getLogger(CustomerFeedbackComponentImpl.class);
	private Gson gson;
	@Resource(name = "customerFeedbackService")
	private CustomerFeedbackService customerFeedbackService;

	public CustomerFeedbackComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean save(String body, Head head) {
		CustomerVo vo = gson.fromJson(body, CustomerVo.class);
		if (vo != null) {
			CustomerFeedbackInfo feed = new CustomerFeedbackInfo();
			feed.setCustomerId(vo.getCustomerId());
			feed.setFeedbackTitle(vo.getFeedbackTitle());
			feed.setFeedbackContent(vo.getFeedbackContent());
			feed.setFeedbackTime(new Date());
			feed.setId(UUID.randomUUID().toString());
			int r = customerFeedbackService.save(feed);
			if (r > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",客户反馈时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
